Country Migration

Data Analysis Project

This document presents an analysis of the number of people migrating from one country (left) to another (right). The interactive Sankey diagram below visualizes these migration flows, offering insights into the patterns and volume of migration between different countries.

Overview

Show the code
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.0
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
Show the code
library(viridis)
Loading required package: viridisLite
Show the code
library(patchwork)
library(hrbrthemes)
NOTE: Either Arial Narrow or Roboto Condensed fonts are required to use these themes.
      Please use hrbrthemes::import_roboto_condensed() to install Roboto Condensed and
      if Arial Narrow is not on your system, please see https://bit.ly/arialnarrow
Show the code
library(circlize)
========================================
circlize version 0.4.15
CRAN page: https://cran.r-project.org/package=circlize
Github page: https://github.com/jokergoo/circlize
Documentation: https://jokergoo.github.io/circlize_book/book/

If you use it in published research, please cite:
Gu, Z. circlize implements and enhances circular visualization
  in R. Bioinformatics 2014.

This message can be suppressed by:
  suppressPackageStartupMessages(library(circlize))
========================================
Show the code
library(networkD3)

# Load and prepare the data
data <- read.table("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/13_AdjacencyDirectedWeighted.csv", header=TRUE)
data_long <- data %>%
  rownames_to_column() %>%
  gather(key = 'key', value = 'value', -rowname) %>%
  filter(value > 0)
colnames(data_long) <- c("source", "target", "value")
data_long$target <- paste(data_long$target, " ", sep="")

# Create nodes
nodes <- data.frame(name=c(as.character(data_long$source), as.character(data_long$target)) %>% unique())

# Create source and target IDs
data_long$IDsource <- match(data_long$source, nodes$name) - 1
data_long$IDtarget <- match(data_long$target, nodes$name) - 1

# Define color scale
ColourScal <- 'd3.scaleOrdinal().range(["#FDE725FF","#B4DE2CFF","#6DCD59FF","#35B779FF","#1F9E89FF","#26828EFF","#31688EFF","#3E4A89FF","#482878FF","#440154FF"])'

# Create the Sankey diagram
sankeyNetwork(Links = data_long, Nodes = nodes,
              Source = "IDsource", Target = "IDtarget",
              Value = "value", NodeID = "name", 
              sinksRight=FALSE, colourScale=ColourScal, nodeWidth=40, fontSize=13, nodePadding=20)

Key Insights

Migration Patterns: Observe which countries are the most common sources and destinations for migrants.

Volume of Migration: Identify the countries with the highest number of migrants.

Regional Trends: Analyze the regional trends and how migration flows vary between different parts of the world.

Detailed Analysis

To dive deeper into the data, we use various visualization techniques and data summarizations:

Migration Flows

Show the code
# Plot migration flows
library(ggplot2)

migration_flow <- data_long %>%
  ggplot(aes(x=source, y=value, fill=target)) +
  geom_bar(stat="identity") +
  theme_minimal() +
  labs(title="Migration Flows by Country", x="Source Country", y="Number of Migrants") +
  theme(axis.text.x = element_text(angle = 90, hjust = 1))

print(migration_flow)

Top Migrating Countries

Show the code
# Summarize top migrating countries
top_migrating_countries <- data_long %>%
  group_by(source) %>%
  summarize(total_migrants = sum(value)) %>%
  arrange(desc(total_migrants)) %>%
  head(10)

# Display in a table
library(DT)

Attaching package: 'DT'
The following object is masked from 'package:networkD3':

    JS
Show the code
datatable(top_migrating_countries, options = list(pageLength = 5))

Conclusion

The migration data reveals significant trends and patterns in global migration. By leveraging data visualization tools, we can uncover valuable insights that help understand the movement of people across countries.

Feel free to explore the interactive elements and delve deeper into the analysis.